/**
* 交易失败
*/
;(function(){
  'use strict';
  angular.module('services.omdisplay.failedTrade',[])
    .factory('failedTradServer',failedTradServer)
    .factory('FailedStatics',FailedStatics)
    .factory('FailedLogic',FailedLogic);
    function failedTradServer(Restangular,FailedStatics){
    return {
        queryfailureTradeStatistics:function(param,callback){
           Restangular.all('sas/OMdisplay/upayQM/businessQM/failureTradeStatistics').post(param).then(callback);
         },
         accessChannelsQuery: function (params,callback) {
          Restangular.all('sas/common/accessChannelsQuery').post(params).then(callback);
        },
        failedtransacTion:function(){
          var items = {
            businessLine:[]
          }
          Boolean(FailedStatics.failedTrade.businessLine);
          if(FailedStatics.failedTrade.businessLine){
            items.businessLine.push(FailedStatics.failedTrade.businessLine.businessLineCode); 
          }else {
            items.businessLine = [];
          }
           Restangular.all('sas/OMdisplay/failed/tradeTypeQuery').post(items).then(
            function(res){
              FailedStatics.failedTrade.getTransactions=is.equal(res.state, '10000') ? res.data : [];
            }
          );
          return FailedStatics;
        },
    };
  }
  failedTradServer.$inject=['Restangular','FailedStatics'];
  function FailedStatics(){
    return{
      getReturnCode :[
        {'returnCode':'','returnName':'空'},
        {'returnCode':'2A','returnName':'2A'},
        {'returnCode':'3A','returnName':'3A'},
        {'returnCode':'4A','returnName':'4A'},
        {'returnCode':'5A','returnName':'5A'}
      ],
    failedTrade:{
      businessLine:'',
      queryType:false,
      getTransactions:[]
    },
    params:{
      returnCode:''
    },
    yearitem:{
      endTime:'',
      startTime:''
    },
    param:{
      startQuarter:'',
      endQuarter:'',
      timeType:'1'
    }
    };
  }
  function FailedLogic(FailedStatics,$filter){
    return{
      getparamInit:getparamInit,
      timedefault:timedefault,
      timeFormart:timeFormart,
      watchBusinessLine: watchBusinessLine
    };
    function watchBusinessLine(value){
      var params={};
      if(value ==='' || value === undefined){
        params.disabledSw=false;
        params.conditionSW=false;
      }else{
       params.disabledSw=true;
        if(value.businessLineName.indexOf('银行总对总')>=0){
          params.conditionSW= true;
        }else{
          params.conditionSW= false;
        }
      }
      return params;
    }
    function getparamInit(){
      FailedStatics.failedTrade={
        businessLine:'',
        queryType:false,
        getTransactions:[]
      };
       FailedStatics.params={
        returnCode:''
      };
        FailedStatics.param={
          timeType:'1'
      };
    }
    /** [timedefault 设置时间默认值,传入账期,设置不同的默认值] */
  function timedefault(value){
    var year=new  Date().getFullYear();
    var month=new Date().getMonth();
    var day=new Date().getDate(); 
    if (value !== '4') {
      FailedStatics.param.startQuarter = undefined;
      FailedStatics.param.endQuarter = undefined;
    }
    FailedStatics.yearitem.endTime = value==='5' ? year : value==='3' ? new Date(year,month) : new Date(year,month,day-1);
    FailedStatics.yearitem.startTime = FailedStatics.yearitem.endTime;
  }

  /**
   * [timeFormart 发送后台时间格式转化]
   * @param  {[type]} formatType [转化格式]
   * @param  {[type]} data       [数据]
   * @return {[type]}            [转换后的格式]
   */
  function timeFormart(value,data){
    var formatType=value==='5' ? 'yyyy' : value==='3' ? 'yyyy-MM' : 'yyyy-MM-dd';
      return $filter('date')(new Date(Date.parse(data)), formatType);
    }
  }
  FailedLogic.$inject=['FailedStatics','$filter'];
})();

